package com.zdc.encrypt;

import org.springframework.boot.env.PropertySourceLoader;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * @author zdc
 * @description InMemoryPropertySourceLoader
 * @date 2025/8/14 9:58
 */
public class InMemoryPropertySourceLoader implements PropertySourceLoader {

    private static Map<String, String> inMemoryProperties;

    public static void setInMemoryProperties(Map<String, String> properties) {
        inMemoryProperties = properties;
    }

    @Override
    public String[] getFileExtensions() {
        return new String[]{"yaml", "yml"};
    }

    @Override
    public List<PropertySource<?>> load(String name, Resource resource) throws IOException {
        if (inMemoryProperties != null && !inMemoryProperties.isEmpty()) {
            Properties props = new Properties();
            props.putAll(inMemoryProperties);
            return Collections.singletonList(new PropertiesPropertySource(name, props)
            );
        }
        return Collections.emptyList();
    }
}